package com.sfh.tourist.base;

import android.graphics.Bitmap;
import android.net.Uri;
import android.support.annotation.DimenRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;

import java.io.File;

public class ImageLoaderEngine {
    public interface ImageCallback {
        void onSuccess();

        void onError(Exception e);
    }

    RequestCreator creator;

    public ImageLoaderEngine(@Nullable Uri uri) {
        this.creator = Picasso.get().load(uri);
    }

    public ImageLoaderEngine(@Nullable String path) {
        this.creator = Picasso.get().load(path);
    }

    public ImageLoaderEngine(@NonNull File file) {
        this.creator = Picasso.get().load(file);
    }

    public ImageLoaderEngine(@DrawableRes int resourceId) {
        this.creator = Picasso.get().load(resourceId);
    }

    public ImageLoaderEngine noPlaceholder() {
        this.creator.noPlaceholder();
        return this;
    }

    public ImageLoaderEngine placeholder(@DrawableRes int placeholderResId) {
        this.creator.placeholder(placeholderResId);
        return this;
    }


    public ImageLoaderEngine error(@DrawableRes int errorResId) {
        this.creator.error(errorResId);
        return this;
    }

    public ImageLoaderEngine resize(int targetWidth, int targetHeight) {
        this.creator.resize(targetWidth, targetHeight);
        return this;
    }

    public ImageLoaderEngine resizeDimen(@DimenRes int targetWidthResId, @DimenRes int targetHeightResId) {
        this.creator.resizeDimen(targetWidthResId, targetHeightResId);
        return this;
    }

    public ImageLoaderEngine centerCrop() {
        this.creator.centerCrop();
        return this;
    }

    public ImageLoaderEngine noFade() {
        this.creator.noFade();
        return this;
    }

    public ImageLoaderEngine memoryPolicy(boolean noCache) {
        if (noCache) {
            this.creator.memoryPolicy(MemoryPolicy.NO_STORE, MemoryPolicy.NO_CACHE);
        }
        return this;
    }

    public ImageLoaderEngine config(@NonNull Bitmap.Config config) {
        this.creator.config(config);
        return this;
    }
    public ImageLoaderEngine skipMemoryCache(){
        this. memoryPolicy(true);
        return this;
    }
    public void into(ImageView iv) {
        this.creator.into(iv);
    }

    public void into(ImageView iv, ImageCallback callback) {
        this.creator.into(iv, new ImageCallbackImpl(callback));
    }
    public void into(ImageView iv, com.squareup.picasso.Callback callback) {
        this.creator.into(iv, callback);
    }

    class ImageCallbackImpl implements com.squareup.picasso.Callback {

        ImageCallback callback;

        public ImageCallbackImpl(ImageCallback callback) {
            this.callback = callback;
        }

        @Override
        public void onSuccess() {
            if (this.callback == null) {
                return;
            }
            this.callback.onSuccess();
        }

        @Override
        public void onError(Exception e) {
            if (this.callback == null) {
                return;
            }
            this.callback.onError(e);
        }
    }
}
